package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;

import com.example.myapplication.R;
import com.example.myapplication.adapter.LinearAdapter;
import com.example.myapplication.util.JsonUtil;
import com.example.myapplication.util.OkHttpUtil;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RecyclerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        //获取元素
        RecyclerView recyclerView1 = findViewById(R.id.recycler1);
        RecyclerView recyclerView2 = findViewById(R.id.recycler1);
        RecyclerView recyclerView3 = findViewById(R.id.recycler1);
        //设置recycler 布局方式
        recyclerView1.setLayoutManager(new LinearLayoutManager(RecyclerActivity.this));
        recyclerView2.setLayoutManager(new GridLayoutManager(RecyclerActivity.this,3));
        recyclerView3.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.HORIZONTAL));
        //设置 Adapter 内容
//        recyclerView1.setAdapter();
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtil.okGet("/userAll", new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String string = response.body().string();
                        List<Map> listMap = JsonUtil.getListMap(string);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                recyclerView1.setAdapter(new LinearAdapter(RecyclerActivity.this,listMap));
                            }
                        });
                    }
                });
            }
        }).start();
    }
}